package com.liyujie.dyeingmachine.ui.home;

import android.os.Bundle;

import androidx.lifecycle.ViewModelProvider;

import com.liyujie.dyeingmachine.R;
import com.liyujie.dyeingmachine.databinding.ActivityHomeBinding;
import com.liyujie.dyeingmachine.mobile.event.MessageEvent;
import com.liyujie.dyeingmachine.ui.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class HomeActivity extends BaseActivity<ActivityHomeBinding> {

    /**
     * VM
     */
    private HomeViewModel homeViewModel;


    @Override
    public void initCreate() {
        super.initCreate();
        //注册，重复注册会导致崩溃
        EventBus.getDefault().register(this);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {

        return R.layout.activity_home;
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(MessageEvent mMessageEvent) {
        switch (mMessageEvent.getType()) {
            /**
             * 开启成功
             * 更新页面
             */
            case OPEN_SUCCESSFILLY:
                /**
                 * 刷新页面
                 */
                initData();
                break;
        }
    }


    @Override
    public void initViewModel() {
        super.initViewModel();
        //VM
        homeViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(HomeViewModel.class);
        binding.setViewModel(homeViewModel);
        /**
         * 初始化
         */
        homeViewModel.initializeViewModel();

    }

    @Override
    public void initView() {
        super.initView();

    }


    @Override
    public void initData() {
        super.initData();


    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();


    }


    @Override
    protected void onDestroy() {
        /**
         * 解绑
         */
        EventBus.getDefault().unregister(this);
        super.onDestroy();

    }
}
